package com.xxgc.spring.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

//Spring的核心配置类
@Configuration
@ComponentScan("com.xxgc.spring")
@PropertySource({"classpath:init.properties","classpath:jdbc.properties"})
public class SpringConfiguration {
    @Value("${jdbc.test.driver}")
    private String driver;
    @Value("${jdbc.test.url}")
    private String url;
    @Value("${jdbc.test.username}")
    private String username;
    @Value("${jdbc.test.password}")
    private String password;

    @Bean("dataSource")
    public DataSource getDataSource() throws PropertyVetoException {
        //实例化数据源并配置
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    }

}
